package cn.nfwx.tv.controller;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;

import java.lang.ref.WeakReference;

import cn.nfwx.tv.base.NFWXApplication;
import cn.nfwx.tv.callback.ResponseWithFailListener;
import cn.nfwx.tv.fragment.bus.BusSceneFragment;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.helper.DialogFragmentHelper;
import cn.nfwx.tv.manager.UserManager;
import cn.nfwx.tv.util.LogUtil;

public class BusAnimController implements ResponseWithFailListener<String>, Handler.Callback {

    public final static int STATS_BUS_IN = 1;
    public final static int STATS_BUS_LEAVE = 2;

    private static final String tag = "bus";

    private String requestAuthCommand, requestRefreshAuthCommand;


    private static BusAnimController instance;

    private String mac;

    public static BusAnimController getInstance() {
        if (instance == null) {
            synchronized (BusAnimController.class) {
                if (instance == null) {
                    instance = new BusAnimController();
                }
            }
        }
        return instance;
    }

    /**
     * @param fragmentManager
     * @param onAnimationFinishListener
     */
    public static void leaveBus(FragmentManager fragmentManager, BusSceneFragment.OnAnimationFinishListener onAnimationFinishListener) {
        BusSceneFragment busSceneFragment = BusSceneFragment.newInstance(STATS_BUS_LEAVE);
        busSceneFragment.setOnAnimationFinishListener(onAnimationFinishListener);
        DialogFragmentHelper.showDialogFragment(fragmentManager, busSceneFragment, tag);
    }

    /**
     * @param fragmentManager
     * @param onAnimationFinishListener
     */
    public static void intBus(FragmentManager fragmentManager, BusSceneFragment.OnAnimationFinishListener onAnimationFinishListener) {
        BusSceneFragment busSceneFragment = BusSceneFragment.newInstance(STATS_BUS_IN);
        busSceneFragment.setOnAnimationFinishListener(onAnimationFinishListener);
        DialogFragmentHelper.showDialogFragment(fragmentManager, busSceneFragment, tag);
    }

    private Handler mHandler = new Handler(this);

    /**
     * 认证
     *
     * @param context
     */
    public void auth(Context context) {
        if (TextUtils.isEmpty(AccountHelper.getInstance().getPhone(context))) {
            return;
        }
        requestAuthCommand = UserManager.requestBusAuth(context,
                AccountHelper.getInstance().getPhone(context),
                "88888888", this);
    }

    /**
     * 刷新认证
     */
    public void refreshAuth() {
        requestRefreshAuthCommand = UserManager.requestRefreshBusAuth(NFWXApplication.getInstance(), mac, this);
    }

    /**
     * 进入公交
     */
    public void requestBus() {
        UserManager.requestQpopBus(NFWXApplication.getInstance(), null);
    }

    @Override
    public void onResponse(String requestTag, String response) {
        if (requestAuthCommand.equals(requestTag)) {
            mac = response.substring(response.indexOf("&mac=") + "&mac=".length(), response.indexOf("&v="));
            mHandler.removeCallbacksAndMessages(null);
            mHandler.sendEmptyMessageDelayed(0, 60000 * 5);
            requestBus();
        } else if (requestRefreshAuthCommand.equals(requestTag)) {
            mHandler.sendEmptyMessageDelayed(0, 60000 * 5);
        }
    }

    @Override
    public void onError(String requestTag, Throwable t) {
        mHandler.removeCallbacksAndMessages(null);
        mHandler.sendEmptyMessageDelayed(1, 60000 * 5);
    }

    @Override
    public void onFinish() {

    }

    @Override
    public void onFail(String requestTag, String response) {
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                refreshAuth();
                break;
            case 1:
                auth(NFWXApplication.getInstance());
                break;
        }
        return false;
    }

    public void release() {
        mHandler.removeCallbacksAndMessages(null);
    }
}
